package backend.server;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import backend.core.transport.Decoder;
import backend.core.transport.Encoder;
import backend.core.transport.Package;

import java.util.List;

/**
 *
 */
public class LiveDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

        Encoder encoder = Encoder.getInstance();
        Decoder decoder = Decoder.getInstance();

        int readableBytes = in.readableBytes();
        byte[] bytes = new byte[readableBytes];
        in.readBytes(bytes);

        // 16进制数据预处理
        String line = new String(bytes);
        System.out.println(line);
        byte[] bytes1 = decoder.hexDecode(line);
        Package decode = encoder.decode(bytes1);

        out.add(decode);
    }

}